<template>
    <div>
        <h1>商品增加</h1>
        {{ info }}
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td align="right">商品名称</td>
                    <td><input type="text" v-model="info.Name"></td>
                </tr>
                <tr>
                    <td align="right">商品编码</td>
                    <td><input type="text" v-model="info.Core"></td>
                </tr>
                <tr>
                    <td align="right">分类</td>
                    <td>
                        <select v-model="info.FLId">
                            <option value="">请选择</option>
                            <option :value="itme.Id" v-for="itme in lisr" :key="itme.Id">{{ itme.FLName }}</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td align="right">图片</td>
                    <td><input type="file" @change="imgs">
                        <img :src="info.Img" width="100" height="100" v-if="info.Img != ''">
                    </td>
                </tr>
                <tr>
                    <td align="right">售卖时间</td>
                    <td><input type="radio" name="str" v-model="info.State" :value="true">上架
                        <input type="radio" name="str" v-model="info.State" :value="false">下架
                    </td>
                </tr>
                <tr>
                    <td align="right">商品备注</td>
                    <td><input type="text" v-model="info.ShopBZ"></td>
                </tr>
                <tr>
                    <td align="right">价格</td>
                    <td><input type="text" v-model="info.Money"></td>
                </tr>
                <tr>
                    <td align="right"></td>
                    <td><input type="button" value="添加" @click="ok"></td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import axios from 'axios';
import { useRouter } from 'vue-router';
const rouder = useRouter();
let info: any = reactive({
    Name: '',
    Core: '',
    FLId: '',
    Img: '',
    State: '',
    Money: '',
    ShopBZ: ''
})
let lisr: any = ref([]);
onMounted(() => {
    getlisr();
})
const getlisr = () => {
    axios({
        url: '/api/Shoping/GetFL',
        method: 'get'
    })
        .then((res) => {
            console.log(res);
            lisr.value = res.data
        })
        .catch((err) => {
            console.log(err);

        })
}

const imgs = (e: any) => {
    let obj = e.target.files[0];
    let f = new FormData();
    f.append('file', obj)
    axios({
        url: '/api/Shoping/Img',
        method: 'post',
        data: f
    })
        .then((res) => {
            console.log(res);
            info.Img = res.data

        })
        .catch((err) => {
            console.log(err);

        })
}

const ok = () => {
    axios({
        url: '/api/Shoping/Add',
        method: 'post',
        data: info
    })
        .then((res) => {
            console.log(res);
            if (res.data == -1) {
                alert('商品名称重复');
                return;
            }
            if (res.data > 0) {
                alert('添加成功');
                rouder.push('/showshoping')
                return;
            }
            else {
                alert('添加失败');
                return;
            }
        })
        .catch((err) => {
            console.log(err);

        })
}
</script>

<style scoped></style>